<?php

class Admin_RoleController extends Zend_Controller_Action
{
	private $_auth;
	private $_acl;
    public function init(){
        $acl                = new My_Acl();
        $auth               = $acl->checkAllowed($this->_request);
        $this->_auth        = $auth->getIdentity();
        $this->view->auth   = $this->_auth;
        $this->_acl         = $acl;
        $this->view->acl    = $this->_acl;
    }
    public function indexAction()
    {
        $modelRole          = new Admin_Model_Role();
        $roleList           = $modelRole->getAllData()->toArray();
        $this->view->roleList = $roleList;
    }
    public function deleteAction()
    {
        if($this->getRequest()->isPost()){
            $del = $this->getRequest()->getPost('del');
            if($del == 'Có'){
                $roleId = $this->getRequest()->getPost('roleId');
                $modelRole = new Admin_Model_Role();
                $roleCheck = $modelRole->getDataByRoleId($id);
                $modelRole->deleteData($roleId);
                $modelUserrole = new Admin_Model_Userrole();
                $modelUserrole->deleteDataByRoleId($roleId);
                // ghi log
                if(file_exists(CONFIG_PATH . '/history.ini')){
                    $configHistory = new Zend_Config_Ini(CONFIG_PATH . '/history.ini', APPLICATION_ENV);
                    
                    $dataHistory['userId']      = $this->_auth['userId'];
                    $dataHistory['time']        = time();
                    $dataHistory['url']         = $this->getRequest()->getRequestUri();
                    $dataHistory['module']      = 'admin';
                    $dataHistory['controller']  = 'role';
                    $dataHistory['action']      = 'delete';
                    $dataHistory['obj']         = 'role';
                    $dataHistory['objField']    = $roleCheck['roleName'];
                    $dataHistory['objId']       = $roleId;
                    $modelHistory = new Admin_Model_History();
                    $modelHistory->addData($dataHistory);
                }
            }
            $this->_helper->redirector('index', 'role', 'admin');
        }else{
            $id = $this->_getParam('id', 0);
            $modelRole = new Admin_Model_Role();
            $this->view->roleCheck = $modelRole->getDataByRoleId($id);
        }
    }
    public function addAction()
    {
        $configApp = new Zend_Config_Ini(CONFIG_PATH . '/application.ini', APPLICATION_ENV);
        $this->view->configApp = $configApp;
        if($this->getRequest()->isPost()){
            $formData = $this->getRequest()->getPost();
            if($formData){
                $gameId = '';
                $resourceId = '';
                $homepage = '';
                $viewPhone = 0;
                $viewEmail = 0;
                if(isset($formData['roleName']))
                    $roleName = $formData['roleName'];
                if(isset($formData['gameId']))
                    $gameId = $formData['gameId'];
                if(isset($formData['resourceId']))
                    $resourceId = $formData['resourceId'];
                if(isset($formData['homepage']))
                    $homepage = $formData['homepage'];
                if(isset($formData['channelDisabled']))
                    $channelDisabled = $formData['channelDisabled'];
                if(isset($formData['viewPhone']))
                    $viewPhone = $formData['viewPhone'];
                if(isset($formData['viewEmail']))
                    $viewEmail = $formData['viewEmail'];
                if($roleName){
                    $modelRole = new Admin_Model_Role();
                    $roleCheck_name = $modelRole->getDataByRoleName($roleName);
                    if(!$roleCheck_name){
                        $roleId = $modelRole->addData($roleName, $homepage, json_encode($gameId), json_encode($resourceId), json_encode($channelDisabled), $viewPhone, $viewEmail);
                        if($roleId){
                            // ghi log
                            if(file_exists(CONFIG_PATH . '/history.ini')){
                                $configHistory = new Zend_Config_Ini(CONFIG_PATH . '/history.ini', APPLICATION_ENV);
                                
                                $dataHistory['userId']      = $this->_auth['userId'];
                                $dataHistory['time']        = time();
                                $dataHistory['url']         = $this->getRequest()->getRequestUri();
                                $dataHistory['module']      = 'admin';
                                $dataHistory['controller']  = 'role';
                                $dataHistory['action']      = 'add';
                                $dataHistory['obj']         = 'role';
                                $dataHistory['objField']    = $roleName;
                                $dataHistory['objId']       = $roleId;
                                $modelHistory = new Admin_Model_History();
                                $modelHistory->addData($dataHistory);
                            }
                            $this->_helper->redirector('index', 'role', 'admin');
                        }else{
                            $this->view->mess = "Đã có lỗi sảy ra. Vui lòng thử lại!";
                            $this->view->flag = "error";
                       }
                    }else{
                        $this->view->mess = "Tên quyền đã tồn tại. Vui lòng chọn tên khác!";
                        $this->view->flag = "error";
                    }
                }else{
                    $this->view->mess = "Vui lòng điền đẩy đủ thông tin!";
                    $this->view->flag = "error";
                }
            }else{
                $this->_helper->redirector('index', 'role', 'admin');
            }
        }
        // mảng tất cả dự án
        $modelGame      = new Admin_Model_Game();
        $gameList    = $modelGame->getAllData();
        $this->view->gameList = $gameList;
        
        // mảng tất cả resource
        $modelResource  = new Admin_Model_Resource();
        $resourceList   = $modelResource->getAllData();
        $this->view->resourceList = $resourceList;
        
    }
    public function editAction()
    {
        $configApp = new Zend_Config_Ini(CONFIG_PATH . '/application.ini', APPLICATION_ENV);
        $this->view->configApp = $configApp;
        $roleId = (int)($this->_getParam('id', 0));
        $modelRole = new Admin_Model_Role();
        $roleCheck = $modelRole->getDataByRoleId($roleId);
        $this->view->roleCheck = $roleCheck;
        if($roleCheck){
            if($this->getRequest()->isPost()){
                $formData = $this->getRequest()->getPost();
                if($formData){
                    $dataUpdate['gameId'] = '';
                    $dataUpdate['resourceId'] = '';
                    $dataUpdate['homepage'] = '';
                    $dataUpdate['viewPhone'] = 0;
                    $dataUpdate['viewEmail'] = 0;
                    if(isset($formData['roleName']))
                        $roleName = $formData['roleName'];
                    if(isset($formData['gameId']))
                        $dataUpdate['gameId'] = json_encode($formData['gameId']);
                    if(isset($formData['resourceId']))
                        $dataUpdate['resourceId'] = json_encode($formData['resourceId']);
                    if(isset($formData['homepage']))
                        $dataUpdate['homepage'] = $formData['homepage'];
                    if(isset($formData['channelDisabled']))
                        $dataUpdate['channelDisabled'] = json_encode($formData['channelDisabled']);
                    if(isset($formData['viewPhone']))
                        $dataUpdate['viewPhone'] = $formData['viewPhone'];
                    if(isset($formData['viewEmail']))
                        $dataUpdate['viewEmail'] = $formData['viewEmail'];
                    
                    if($roleName){
                        $roleCheck_name = $modelRole->getDataByRoleName($roleName);
                        if(!$roleCheck_name || $roleCheck_name['roleId'] == $roleCheck['roleId']){
                            $modelRole->updateData($roleId, $roleName, $dataUpdate['homepage'], $dataUpdate['gameId'], $dataUpdate['resourceId'], $dataUpdate['channelDisabled'], $dataUpdate['viewPhone'], $dataUpdate['viewEmail']);
                            // ghi log
                            if(file_exists(CONFIG_PATH . '/history.ini')){
                                $configHistory = new Zend_Config_Ini(CONFIG_PATH . '/history.ini', APPLICATION_ENV);
                                
                                $dataHistory['userId']      = $this->_auth['userId'];
                                $dataHistory['time']        = time();
                                $dataHistory['url']         = $this->getRequest()->getRequestUri();
                                $dataHistory['module']      = 'admin';
                                $dataHistory['controller']  = 'role';
                                $dataHistory['action']      = 'edit';
                                $dataHistory['obj']         = 'role';
                                $dataHistory['objField']    = $roleName;
                                $dataHistory['objId']       = $roleId;
                                $dataHistory['valueField']  = array();
                                $dataHistory['valueOld']    = array();
                                $dataHistory['valueNew']    = array();
                                foreach($dataUpdate as $key=>$value){
                                    if(isset($roleCheck[$key]) && $value != $roleCheck[$key]){
                                        $dataHistory['valueField'][] = $key;
                                        $dataHistory['valueOld'][] = $roleCheck[$key];
                                        $dataHistory['valueNew'][] = $value;
                                    }
                                }
                                if($dataHistory['valueField']){
                                    $dataHistory['valueField']  = json_encode($dataHistory['valueField']);
                                    $dataHistory['valueOld']    = json_encode($dataHistory['valueOld']);
                                    $dataHistory['valueNew']    = json_encode($dataHistory['valueNew']);
                                    $modelHistory = new Admin_Model_History();
                                    $modelHistory->addData($dataHistory);
                                }
                            }
                            $this->_helper->redirector('index', 'role', 'admin');
                        }else{
                            $this->view->mess = "Tên quyền đã tồn tại. Vui lòng chọn tên khác!";
                            $this->view->flag = "error";
                        }
                    }else{
                        $this->view->mess = "Vui lòng điền đẩy đủ thông tin!";
                        $this->view->flag = "error";
                    }
                }else{
                    $this->_helper->redirector('index', 'role', 'admin');
                }
            }else{
                
                // mảng tất cả dự án
                $modelGame      = new Admin_Model_Game();
                $gameList       = $modelGame->getAllData();
                $this->view->gameList = $gameList;
                
                // mảng tất cả resource
                $modelResource  = new Admin_Model_Resource();
                $resourceList   = $modelResource->getAllData();
                $this->view->resourceList = $resourceList;
            }
        }else{
            $this->_helper->redirector('index', 'role', 'admin');
        }
    }
}







